//
//  HYFileDownloadView.m
//  XPlayer
//
//  Created by liguoqing on 2023/10/13.
//

#import "HYFileDownloadView.h"
#import "HYFileDownloadViewCell.h"
#import <HYUIKit/HYAlertShowAnimate.h>
#import "HYFileDownloadViewController.h"
#import "HYSettingHeadView.h"

@interface HYFileDownloadView ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, assign) BOOL isFull;

@property (nonatomic, strong) NSString *folderIdentification;

@property (nonatomic, strong) HYFileDownloadViewModel *viewModel;

@property (nonatomic, strong) UITableView *tableView;
// 查看全部缓存
@property (nonatomic, strong) UIButton *showAllBtn;

@property (nonatomic, copy) dispatch_block_t disappearBlock;

@end

@implementation HYFileDownloadView

- (void)dealloc
{
    CLog(@"delloc -- %@",[self class]);
}

- (instancetype)initWithViewModel:(HYFileDownloadViewModel *)viewModel
{
    self = [super init];
    if(self){
        self.viewModel = viewModel;
        [self configUI];
        [self bindSignal];
    }
    return self;
}

- (instancetype)initWithFolderIdentificaiton:(NSString *)folderIdentification
                                      isFull:(BOOL)isFull
                                   disappear:(void(^)(void))disappear
{
    self = [super init];
    if(self){
        self.folderIdentification = folderIdentification;
        self.isFull = isFull;
        self.disappearBlock = disappear;
        self.viewModel = [[HYFileDownloadViewModel alloc]initWithFolderIdentification:folderIdentification];
        [self configUI];
        [self loadData];
        [self bindSignal];
    }
    return self;
}

- (void)configUI
{
    self.backgroundColor = AppBgThemeColor;
    if(self.folderIdentification){
        if(!self.isFull){
            [self addSubview:self.showAllBtn];
            [self.showAllBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self);
                make.width.mas_equalTo(90);
                make.height.mas_equalTo(22);
                make.bottom.mas_equalTo(-HOME_BAR_HEIGHT-5);
            }];
        }
        [self addSubview:self.tableView];
        if(self.isFull){
            [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(5);
                make.left.right.bottom.equalTo(self);
            }];
        }else{
            [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(5);
                make.left.right.bottom.equalTo(self);
                make.bottom.equalTo(self.showAllBtn.mas_top).offset(-5);
            }];
        }
    }else{
        [self addEmptyViewWithPlaceImageName:@"icon_empty_content" placeTitle:NSLocalizedString(@"暂无下载数据", nil)];
        [self addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
    }
}
- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.downCompletdSubject subscribeNext:^(id x) {
        @strongify(self);
        [self loadData];
    }];
}

- (void)loadData
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.viewModel loadData];
        [self.tableView reloadData];
    });
}

- (void)closeBtnAction
{
    [HYAlertShowAnimate disappear];
}

- (void)show;
{
    self.frame = CGRectMake(0, 0,self.isFull?kScreenHeight:kScreenWidth,self.isFull?kScreenHeight:kScreenHeight/2);
    [HYAlertShowAnimate showInView:[UIApplication sharedApplication].keyWindow
                         alertView:self
                         popupMode:self.isFull?View_Popup_Mode_Right:View_Popup_Mode_Down
                           bgAlpha:0.3
                  outsideDisappear:YES disappear:self.disappearBlock];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger section = [self.viewModel numberOfSectionsInTableView];
    tableView.hidden = !section;
    return section;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.viewModel numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYFileDownloadViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HYFileDownloadViewCell" forIndexPath:indexPath];
    [cell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kItemListHeight+10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if(!self.folderIdentification){
        [self.viewModel didSelectRowAtIndexPath:indexPath];
    }
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.folderIdentification?NO:YES;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HYSettingHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CELLIDHYSettingHeadView"];
    NSString *headString = [self.viewModel headerTitleForHeaderInSection:section];
    [headView setHeadTitle:headString withTextAlignment:NSTextAlignmentLeft];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]init];
    return view;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    @weakify(self);
    if(editingStyle == UITableViewCellEditingStyleDelete){
        @strongify(self);
        BOOL result = [self.viewModel deleteDownloadTashAtIndexPath:indexPath];
        if(result){
            [self loadData];
        }
    }
}


- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)showAllBtnAction
{
    [HYAlertShowAnimate disappear];
    HYFileDownloadViewController *downloadVC = [[HYFileDownloadViewController alloc]init];
    [[CommUtls getCurrentVC].navigationController pushViewController:downloadVC animated:YES];
}

#pragma mark - loazyLoad
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = AppBgThemeColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[HYFileDownloadViewCell class] forCellReuseIdentifier:@"HYFileDownloadViewCell"];
        [_tableView registerClass:[HYSettingHeadView class] forHeaderFooterViewReuseIdentifier:@"CELLIDHYSettingHeadView"];
    }
    return _tableView;
}

- (UIButton *)showAllBtn
{
    if(!_showAllBtn){
        _showAllBtn = [[UIButton alloc]init];
        [_showAllBtn setTitle:NSLocalizedString(@"查看全部缓存", nil) forState:UIControlStateNormal];
        _showAllBtn.titleLabel.font = FONT_SIZE(12);
        [_showAllBtn setTitleColor:APPTextThemeColor forState:UIControlStateNormal];
        [_showAllBtn addTarget:self action:@selector(showAllBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _showAllBtn.layer.cornerRadius = 4.f;
        _showAllBtn.layer.borderWidth = kOneScale;
        _showAllBtn.layer.borderColor = APPTextThemeColor.CGColor;
    }
    return _showAllBtn;
}


@end
